import React from 'react';
import {inject, observer} from 'mobx-react';
import {toJS} from 'mobx';

import {
  WeaTop,
  WeaLeftRightLayout,
  WeaLeftTree,
  WeaTools,
  WeaOrgTree,
  WeaTab,
  WeaFormItem,
  WeaSearchGroup,
  WeaRightMenu,
  WeaAlertPage,
  WeaLocaleProvider
} from 'ecCom';
import {Button} from 'antd';
import {WeaTableNew, WeaSwitch} from 'comsMobx';
const {WeaTable} = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;
import * as EsearchUtil from '../../util/EsearchUtil';
import WeesoRobotDialog from './RobotDialog';

@inject('weesoRobotStore')
@observer
export default class Index extends React.Component {
  constructor (props) {
    super(props);
    this.state = {
    }
  }

  componentDidMount() {
    const {weesoRobotStore} = this.props;
    weesoRobotStore.doInit();
  }

  componentWillReceiveProps(nextProps) {
    const {weesoRobotStore} = this.props;
    if (this.props.location.key !== nextProps.location.key) {
      weesoRobotStore.doInit()
    }
  }

  getTabButtonsAd() {
    const {weesoRobotStore} = this.props;
    let btns = btns=[
      <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@9nznmq@doSearch`} type='primary' onClick={() => {
        weesoRobotStore.setState({showSearchAd: false})
        weesoRobotStore.getList({...weesoRobotStore.form.getFormParams()},true);
      }}>{getLabel(197,'搜索')}</Button>,
      <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@fdg16z@doReset`} type="ghost" onClick={() => weesoRobotStore.form.reset()}>{getLabel(2022,'重置')}</Button>,
      <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@fq128i@doCancel`} type="ghost" onClick={() => weesoRobotStore.setState({showSearchAd: false})}>{getLabel(31129,'取消')}</Button>,
    ]
    return btns;
  }

  getRightMenu() {
    const selectedRowKeys = toJS(this.props.weesoRobotStore.tableStore.selectedRowKeys);
    let btnArr = [
      {
        key: 'BTN_NEW',
        icon: <i className='icon-coms-New-Flow'/>,
        content : getLabel(611,'添加'),
        onClick : () => this.onRightMenuClick('BTN_NEW')
      },
      {
        key: 'BTN_DEL',
        icon: <i className='icon-coms-delete'/>,
        disabled:selectedRowKeys.length === 0,
        content : getLabel(32136,'批量删除'),
        onClick : () => this.onRightMenuClick('BTN_DEL')
      },
      {
        key: 'BTN_INDEX',
        icon: <i className='icon-coms-Journal'/>,
        content : getLabel(20472,'创建索引'),
        onClick : () => this.onRightMenuClick('BTN_INDEX')
      },
      {
        key: 'BTN_COLUMN',
        icon: <i className='icon-coms-Custom'/>,
        content : getLabel(32535,'显示列定制'),
        onClick : () => this.onRightMenuClick('BTN_COLUMN')
      }
    ];
    return btnArr;
  }

  getBtns = () => {
    const selectedRowKeys = toJS(this.props.weesoRobotStore.tableStore.selectedRowKeys);
    let btns = [
      <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@vtyc1r@BTN_NEW`} type='primary' onClick={() => this.onRightMenuClick('BTN_NEW')}>{getLabel(611,'添加')}</Button>,
      <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@ne1zw2@BTN_DEL`} type='primary' disabled={selectedRowKeys.length === 0} onClick={() => this.onRightMenuClick('BTN_DEL')}>{getLabel(32136,'批量删除')}</Button>,
      <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@plc2qb@BTN_INDEX`} type='primary' onClick={() => this.onRightMenuClick('BTN_INDEX')}>{getLabel(20472,'创建索引')}</Button>
    ];
    return btns;
  }

  onRightMenuClick = (type) => {
    const {weesoRobotStore} = this.props;
    const selectedRowKeys = toJS(weesoRobotStore.tableStore.selectedRowKeys);
    if(type == 'BTN_NEW'){//新建
      weesoRobotStore.setDialogState({createOrEdit: 0});
      weesoRobotStore.dialogStatus.canClick && weesoRobotStore.getFields();
    }
    if(type == 'BTN_DEL'){//批量删除
      if(selectedRowKeys && selectedRowKeys.length > 0){
        EsearchUtil.doConfirm(`${getLabel(127574, '确定删除吗？')}`, () => weesoRobotStore.deleteRobot({ids: selectedRowKeys.join(',')}));
      }else {
        EsearchUtil.setModal(`${getLabel(22000,'请先选择操作对象')}`);
      }
    }
    if(type == 'BTN_INDEX'){//创建索引
      weesoRobotStore.createRobotIndex();
    }
    if(type == 'BTN_COLUMN'){//显示列定制
      weesoRobotStore.tableStore.setColSetVisible(true);
      weesoRobotStore.tableStore.tableColSet(true);
    }
  }

  onOperatesClick = (record, index, operate) => {
    const {weesoRobotStore} = this.props;
    if(operate.index == 0){//启用
      EsearchUtil.doConfirm(getLabel(83450, '确定启用吗？'), () => weesoRobotStore.openRobot({state: 0, id: record.randomFieldId}))
    }
    if(operate.index == 1){//禁用
      EsearchUtil.doConfirm(getLabel(83451, '确定禁用吗？'), () => weesoRobotStore.openRobot({state: 1, id: record.randomFieldId}))
    }
    if(operate.index == 2){//编辑
      weesoRobotStore.setDialogState({createOrEdit: 1, editId: record.randomFieldId});
      weesoRobotStore.dialogStatus.canClick && weesoRobotStore.getFields({id: record.randomFieldId});
    }
    if(operate.index == 3){//删除
      EsearchUtil.doConfirm(getLabel(127574, '确定删除吗？'), () => weesoRobotStore.deleteRobot({ids: record.randomFieldId}));
    }
  }

  render() {
    const {weesoRobotStore} = this.props;
    const {tableStore, status, form } = weesoRobotStore;
    const {showSearchAd, conditioninfo, alertPage} = toJS(status);
    let collectParams={
        favname: getLabel(82832, '智能指令'),
        favouritetype: 5,
        objid:0,
        importantlevel: 1,
    };
    let title=getLabel(82832, '智能指令');

    return (
      <div style={{height: '100%'}}>
        {alertPage ?
          <WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@mp3n4b`}>
            <div>{getLabel(382663,'暂无权限')}</div>
          </WeaAlertPage>
          :
          <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@r3pnlc`}
            datas={this.getRightMenu()}
            collectParams={collectParams}
          >
            <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@yei22i`}
              title={title}
              icon={<i className='icon-coms-ws' />}
              iconBgcolor='#A0B0BA'
              buttonSpace={10}
              buttons={this.getBtns()}
              showDropIcon={true}
              dropMenuDatas={this.getRightMenu()}
              dropMenuProps={{
                collectParams:collectParams
              }}
            />
              <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@j5v4qp`}
                searchType={['base', 'advanced']}
                searchsAd={EsearchUtil.getSearchs(form, conditioninfo, false, 2, () => {
                  weesoRobotStore.setState({showSearchAd: false});
                  weesoRobotStore.getList({...weesoRobotStore.form.getFormParams()},true);
                })}
                showSearchAd={showSearchAd}
                advanceHeight={220}
                buttonsAd={this.getTabButtonsAd()}
                setShowSearchAd={() => weesoRobotStore.setState({showSearchAd: !showSearchAd})}
                hideSearchAd={() => weesoRobotStore.setState({showSearchAd: false})}
                searchsBaseValue= {form.isFormInit && form.getFormParams() && form.getFormParams().title || ''}
                onSearch={() => weesoRobotStore.getList({...form.getFormParams()}, true)}
                onSearchChange={value => form.updateFields({title : value})}
              />
              <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@68yrwh`}
                comsWeaTableStore={tableStore}
                hasOrder={true}
                needScroll={true}
                onOperatesClick={this.onOperatesClick.bind(this)}
              />
          </WeaRightMenu>
        }
        <WeesoRobotDialog ecId={`${this && this.props && this.props.ecId || ''}_WeesoRobotDialog@3oe2o1`}/>
      </div>
    )
  }
}